home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Xtep / XList.cc next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  2.4 KB  |  126 lines

  1. /* XList.cpp */
  2.  
  3. #include "XList.hh"
  4.  
  5.  
  6. void
  7. CXNode::AddNext(XObject *inNewDatum)
  8. {
  9.     CXNode *node = new CXNode(inNewDatum);
  10.     node->mNext = mNext;
  11.     mNext = node;
  12. }
  13.  
  14. CXNode *
  15. CXNode::RemoveNext()
  16. {
  17.     CXNode *node = mNext;
  18.     mNext = mNext->mNext;
  19.     return node;
  20. }
  21.  
  22. XList::~XList()
  23. {
  24.     Purge();
  25. }
  26.  
  27. CXNode *
  28. XList::FindNode(XObject * inDatum) const
  29. {
  30.     CXNode *node = mHead;
  31.     
  32.     for (node = mHead; node; node = node->Next())
  33.         if (node->Datum() == inDatum)
  34.             return node;
  35.     
  36.     return NULL;
  37. }
  38.  
  39. void
  40. XList::Prepend(XObject * inNewDatum)
  41. {
  42.     if (!mHead) mHead = mTail = new CXNode(inNewDatum);
  43.     else {
  44.         CXNode *head = new CXNode(inNewDatum);
  45.         head->mNext = mHead;
  46.         mHead = head;
  47.     }
  48. }
  49.  
  50. void
  51. XList::Append(XObject *inNewDatum)
  52. {
  53.     if (!mHead) mHead = mTail = new CXNode(inNewDatum);
  54.     else {
  55. #if 0
  56.         CXNode *last = mHead;
  57.         while (last->Next())
  58.             last = last->Next();
  59.         last = new CXNode(inNewDatum);
  60.         mTail = last;
  61. #endif
  62.         mTail->mNext = new CXNode(inNewDatum);
  63.         mTail = mTail->mNext;
  64.     }
  65. }
  66.  
  67. XObject *
  68. XList::Behead()
  69. {
  70.     if (!mHead) return NULL;
  71.     CXNode *head = mHead;
  72.     mHead = mHead->mNext;
  73.     return head->Datum();
  74. }
  75.  
  76. void
  77. XList::Remove(XObject *inVictim)
  78. {
  79.     // Check for an empty list.
  80.     if (!mHead) return;
  81.     
  82.     CXNode *pred = mHead;
  83.     
  84.     // Check for special case: match on first item.
  85.     if (mHead->Datum() == inVictim) {
  86.         mHead = mHead->Next();
  87.         delete pred;
  88.         return;
  89.     }        
  90.     while (pred->Next()) { // List of one item stops here.
  91.         if (pred->Next()->Datum() == inVictim) {
  92.             delete pred->RemoveNext();
  93.             return;
  94.         }
  95.     }
  96.     // Couldn't find it.
  97. }
  98.  
  99. void
  100. XList::Purge()
  101. {
  102.     CXNode *node = mHead;
  103.     
  104.     // It is critical to reset mHead prior to deleting nodes, because it is
  105.     // possible for a destructor to call autorelease(), which would attempt to
  106.     // append a new node onto the list.
  107.     // Actually, Append() would check mHead and conclude that there were nodes,
  108.     // and add the new node to the end of the list, which would actually work,
  109.     // if mTail was not the node being deleted.
  110.     // More actually, mTail would be set to the new node, but the delete loop
  111.     // would have already recorded a NULL for ondeck.  A NULL mHead indicates an
  112.     // empty list, in which case mTail is undefined, so correct code would not
  113.     // subsequently refer to it.  Therefore, any objects autoreleased by the last
  114.     // node being deleted would be leaked.  So reset mHead first!
  115.     
  116.     mHead = NULL;
  117.     
  118.     while (node) {
  119.         CXNode *ondeck = node->Next();
  120.         //XObject *victim = node->Datum();
  121.         delete node;
  122.         //victim->release();
  123.         node = ondeck;
  124.     }
  125. }
  126.